
//  Copyright (c) Microsoft Corporation.  All Rights Reserved.

using System;
using System.Configuration;
using System.ServiceModel;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Microsoft.ServiceModel.Samples
{
    // Define a service contract.
    [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
        [OperationContract]
        int GetTableNumber();
        [OperationContract]
        bool joinTable(string idTable,string player);
        [OperationContract]
        bool createTable();
        [OperationContract]
        bool bet(string idTable, string playerId, int cashAmout);
        [OperationContract]
        string getCurrentBets(string idTable);
        [OperationContract]
        Dictionary<string, PokerTable>.KeyCollection getTables();
        [OperationContract]
        void Sleep();
    }

    // Service class which implements the service contract.
    // Added code to write output to the console window
    [ServiceBehavior(Name = "CalculatorService", Namespace = "http://Microsoft.ServiceModel.Samples",
    ConcurrencyMode = ConcurrencyMode.Multiple,
    InstanceContextMode = InstanceContextMode.Single)]
    public class CalculatorService : ICalculator
    {
        private Dictionary<string, PokerTable> testDict;

        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Divide(double n1, double n2)
        {
            double result = n1 / n2;
            Console.WriteLine("Received Divide({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }


        // Host the service within this EXE console application.
        public static void Main()
        {
            // Create a ServiceHost for the CalculatorService type.
            using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)))
            {
                // Open the ServiceHost to create listeners and start listening for messages.
                serviceHost.Open();

                // The service can now be accessed.
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();
            }
        }

        public CalculatorService()
        {
            testDict = new Dictionary<string, PokerTable>();
        }

        #region PKR Members


        public int GetTableNumber()
        {
            return testDict.Count;
        }

        public bool joinTable(string idTable, string player)
        {
            bool result = false;
            PokerTable tbl = null;
            result = testDict.TryGetValue(idTable, out  tbl);
            if (result)
            {
                tbl.players.Add(new PokerPlayer(player));
                return result;
            }
            else
                return result;
        }

        public bool createTable()
        {
            try
            {
                PokerTable tbl = new PokerTable();
                testDict.Add(tbl.name, tbl);
                return true;
            }
            catch
            { return false; }
        }

        public bool bet(string idTable, string playerId, int cashAmout)
        {
            bool result = false;
            PokerTable tbl = null;
            result = testDict.TryGetValue(idTable, out  tbl);
            if (result)
            {
                result = false;
                foreach (PokerPlayer pl in tbl.players)
                {
                    if (pl.name == playerId)
                    {
                        result = true;
                        pl.bet(cashAmout);
                        break;
                    }
                }
                return result;
            }
            else
                return result;
        }

        public string getCurrentBets(string idTable)
        {
            string result = null;
            PokerTable tbl = null;
            
            if (testDict.TryGetValue(idTable, out  tbl))
            {
                result = "";
                foreach (PokerPlayer pl in tbl.players)
                {
                    result += pl.name + " : " + pl.Bet + "\n";
                }
            }
                return result;
        }

        public Dictionary<string,PokerTable>.KeyCollection getTables()
        {
            return testDict.Keys;
        }

        #endregion

        #region Sopel test Members


        public void Sleep()
        {
            System.Threading.Thread.Sleep(10000);
        }

        #endregion
    }

    [DataContract(Namespace = "http://Microsoft.ServiceModel.Samples", Name = "PokerTable")]
    public class PokerTable
    {

        private String nameField;
        private List<PokerPlayer> playersField;

        public PokerTable()
        {
            name = Guid.NewGuid().ToString();
            players = new List<PokerPlayer>();
        }

        [DataMember(IsRequired = true, Name = "name", Order = 0)]
        public String name
        {
            get { return nameField; }
            set { nameField = value; }
        }

        [DataMember(IsRequired = false, Name = "players", Order = 1)]
        public List<PokerPlayer> players
        {
            get { return playersField; }
            set { playersField = value; }
        }
    }

    [DataContract(Namespace = "http://Microsoft.ServiceModel.Samples", Name = "PokerPlayer")]
    public class PokerPlayer
    {
        private string nameField;
        private int currentBet;

        public void bet(int cash)
        {
            currentBet+=cash;
        }

        [DataMember(IsRequired = true, Name = "Bet", Order = 0)]
        public int Bet
        {
            get { return currentBet; }
            set { currentBet = value; }
        }

        [DataMember(IsRequired = true, Name = "name", Order = 1)]
        public String name
        {
            get { return nameField; }
            set { nameField = value; }
        }


        public PokerPlayer(string Name)
        {
            name = Name;
            currentBet = 0;
        }
    }
}
